<template>
	
	<view class="index">
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		onLoad() {

		},
		methods: {
			/**
			 * 判断当前是否有网络
			 */
			isNetwork() {
				let that  = this;
				uni.getNetworkType({
				    success: function (res) {
				        // console.log('这里请求失败，判断当前是否有网络，没有网络，请离线巡检', res.networkType);
						// res.networkType == 'none' || res.networkType == 'unknown'
						if(res.networkType == 'none'){
							// 无网络
			
						}else{
							// 有网络 获取当前WiFi信号强度
						}
				    },
					fail() {
					}
				});
			},
			
			
			/**
			 * 获取当前WiFi信号强度
			 */
			checkWifiSignal() {
				// console.log('获取当前WiFi信号强度  ')
				// #ifdef APP-PLUS
			
			     let Signal = null;
			     let Context = plus.android.importClass("android.content.Context");
			     let WifiManager = plus.android.importClass("android.net.wifi.WifiManager");
			     let Main = plus.android.runtimeMainActivity();
			     WifiManager = Main.getSystemService(Context.WIFI_SERVICE);
			     let WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo");
			     WifiInfo = WifiManager.getConnectionInfo();
			     let wifi = WifiInfo.getRssi();
			
			     if (wifi > -50 && wifi < 0) { //最强
			         Signal = "最强";
			     } else if (wifi > -70 && wifi < -50) { //较强
			         Signal = "较强";
			     } else if (wifi > -80 && wifi < -70) { //较弱
			         Signal = "较弱";
			     } else if (wifi > -100 && wifi < -80) { //微弱
			         Signal = "微弱";
			     }
				// console.log('WiFi强弱', Signal, wifi)
				if(wifi < -70) {
					uni.showModal({
						title: '网络信号不好',
						content: '当前网络信号不好，会导致数据加载慢，图片不能显示等问题。请到网络好的地方巡检或离线巡检。',
						showCancel: false
					})
				}
			     return Signal;
			
				 // #endif
			},
			
			/**
			 * 将时间转换为日期
			 */
			timestampToTime(timestamp) {
				// console.log('timestamp', timestamp)
				let date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
				// console.log('time ', date)
				let Y = date.getFullYear() + '-';
				let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
				let D = date.getDate() < 10? '0'+date.getDate() : date.getDate();
				// var h = date.getHours() + ':';
				// var m = date.getMinutes() + ':';
				// var s = date.getSeconds();
				return Y + M + D;
			},
			
			
			/**
			 * 获取当前时间
			 */
			getUpdateTime() {
				let time= new getDate()
				let year = time.getFullYear();
				let month = (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
				let day = time.getDate() < 10? '0'+time.getDate() : time.getDate();
				let hours = time.getHours() < 10 ? '0'+time.getHours() : time.getHours();
				let minutes = time.getMinutes() < 10 ? '0'+time.getMinutes() : time.getMinutes();
				let seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
			
				let nowDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
				return nowDate;
			},
			
			
			/**
			 * 判断当天是否更新过离线数据
			 * 当天打开这个页面发现，上次更新的数据的年月日  小于 现在的 年月日，则跟新数据
			 */
			updateOfflineData() {
				let that = this;
				let da  = new Date()
				let year = da.getFullYear()
				let month = da.getMonth() + 1;
				let day = da.getDate()
				// console.log('判断当天是否更新过离线数据', da, year, month, day)
				uni.getStorage({
					key: 'updataOfflineDataTime',
					success(res) {
						console.log('上一次更新数据的时间', res)
						let info = res.data;
						if(year > info.year){
							that.setOfflineData()
						}else if(month >info.month) {
							that.setOfflineData()
						}else if(day > info.day) {
							that.setOfflineData()
						}
					},
					fail() {
						// 获取失败更新数据
						that.setOfflineData()
					}
				})
			},
			
			/**
			 * 设置当天更新数据的时间和状态
			 */
			setSameDayTime() {
				let that = this;
				let da  = new Date()
				let year = da.getFullYear()
				let month = da.getMonth() + 1;
				let day = da.getDate()
			
				uni.setStorage({
					key: 'updataOfflineDataTime',
					data: {
						year: year,
						month: month,
						day: day
					}
				})
			},
			
			/**
			 * 当网络正常时，更新本地离线数据
			 * 同步方式跟新数据，当有一个接口有异常抛出，则跟新失败
			 * 
			 * 当页面中要获取多个接口时，这种方式来做，等待所有接口都执行完后，在操作其他的
			 */
			async setOfflineData() {
				let that = this;
				uni.showLoading({
					title: '正在更新离线数据，请等待',
					mask: true
				})
				try{
					// 更新离线任务数据
					await that.setOfflineTask()
			
					// 更新离线边坡点列表
					await that.getSideDot()
			
					// 获取边坡类型
					await that.getSideType()
			
					// 边坡类型对应的所有题,
					await that.getQuestions()
			
					// 获取养护建议
					await that.getFosterIdea()
			
					// 更新当天更新离线数据的时间,
					that.setSameDayTime()
			
					uni.showToast({
						title: '已完成更新',
						mask: true
					})
				}catch(e){
					// console.log('更新离线数据 异常', e)
			
					uni.showToast({
						title: e,
						mask: true,
						icon: 'none'
					})
				}
			},
			
			
			/**
			 * 养护建议
			 */
			getFosterIdea() {
				// console.log('养护建议 aa')
				let url = '';
				let data = {
					method: 'getDictDetail',
					dictName: '养护建议',
					dictId: 7
				}
				let that = this;
				let p = new Promise((resolve, reject)=>{
			
					this.$requestP(url, data, res=>{
						let arr = res.data.data.dictDetailList;
						for(let i =0; i<arr.length; i++){
							arr[i].flag = false
						}
						let lis = JSON.stringify(arr)
						uni.setStorage({
							key: 'offlineFosterArr',
							data: lis,
							success() {
								// console.log('养护建议  5')
								resolve(true)
							},
							fail() {
								reject('更新养护建议失败')
							}
						})
				
					}, () =>{
						reject('更新养护建议失败')
					}, () =>{
				
					}, () =>{
						reject('更新养护建议失败')
					})
				})
				return p;
			},
			
			
			/**
			 * 根据边坡类型，获取题型
			 * sideType:　边坡类型数组
			 * 请求多个接口，多个接口请求完成后，统一做处理，用这种方法，与上面的效果一样
			 */
			getQuestion(sideType)  {
				let offlineQuestions = []
				let that = this;
				let p1 = new Promise((resolve, reject) => {
					let url ='';
					let data = {
						method: 'getQuestion',
						id: sideType[0].id
					}
					that.$requestP(url, data, res=>{
						let sideQuestion = res.data.data.questionList;
						resolve(sideQuestion)
					}, () =>{
			
					})
				})
				let p2 = new Promise((resolve, reject) => {
					let url ='';
					let data = {
						method: 'getQuestion',
						id: sideType[1].id
					}
					that.$requestP(url, data, res=>{
						let sideQuestion = res.data.data.questionList;
						resolve(sideQuestion)
					}, () =>{
			
					})
				})
				let p3 = new Promise((resolve, reject) => {
					let url ='';
					let data = {
						method: 'getQuestion',
						id: sideType[2].id
					}
					that.$requestP(url, data, res=>{
						let sideQuestion = res.data.data.questionList;
						resolve(sideQuestion)
					}, () =>{
			
					})
				})
				let p4 = new Promise((resolve, reject) => {
					let url ='';
					let data = {
						method: 'getQuestion',
						id: sideType[3].id
					}
					that.$requestP(url, data, res=>{
						let sideQuestion = res.data.data.questionList;
						resolve(sideQuestion)
					}, () =>{
			
					})
				})
				Promise.all([p1,p2,p3,p4]).then(res=>{
					// console.log('根据边坡类型获取到题型 成功', res)
					for(let i =0; i<res.length; i++){
						offlineQuestions.push({
							sideTypeId: sideType[i].id,
							sideTyepName: sideType[i].name,
							question: res[i]
						})
					}
					let lis = JSON.stringify(offlineQuestions)
					// console.log('根据边坡类型获取到题型 成功', offlineQuestions)
			
					uni.setStorage({
						key: 'offlineQuestions',
						data: lis,
						success(){
							// console.log('设置离线题', lis)
						}
					})
				}).catch(err=>{
					console.log(' 有妖气')
				})
			},
		}
	}
</script>

<style lang="scss">
	
</style>
